package com.tronsis.distribution.activity.me.upgrade;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.me.CollectionWayActivity;
import com.tronsis.distribution.activity.me.PaymentVoucherActivity;
import com.tronsis.distribution.bean.PayeeMethodBean;
import com.tronsis.distribution.bean.RulesMapBean;
import com.tronsis.distribution.bean.UpgradeBean;
import com.tronsis.distribution.bean.UpgradeDetailBean;
import com.tronsis.distribution.bean.UploadFileBean;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.CommonFactory;
import com.tronsis.distribution.biz.factory.FileBizFactory;
import com.tronsis.distribution.biz.factory.UpgradeBizFactory;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.distribution.util.CommonUtil;
import com.tronsis.distribution.util.StateUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.ImageLoader;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.util.WindowUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.lzy.imagepicker.ImagePicker.REQUEST_CODE_PREVIEW;

public class LevelApplyDetailActivity extends ToolBarBaseActivity {
    private static final int REQUEST_CAMERA = 125;
    private static final int REQUEST_ALBUM = 126;
    private static final int REQUEST_VIDEO = 127;

    @BindView(R.id.tv_order_code)
    TextView mTvOrderCode;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.tv_level)
    TextView mTvLevel;
    @BindView(R.id.tv_region)
    TextView mTvRegion;
    @BindView(R.id.tv_payee)
    TextView mTvPayment;
    @BindView(R.id.ll_payee_info)
    LinearLayout mLlPayeeInfo;
    @BindView(R.id.ll_pay_type)
    LinearLayout mLlCollectionWay;
    @BindView(R.id.tv_deposit_price)
    TextView mTvDepositPrice;
    @BindView(R.id.tv_pay_wait)
    TextView mTvPayWait;
    @BindView(R.id.tv_refund_price)
    TextView mTvRefundPrice;
    @BindView(R.id.tv_use_points)
    TextView mTvUsePoints;
    @BindView(R.id.et_points)
    EditText mEtPoints;
    @BindView(R.id.tv_pay_actual)
    TextView mTvPayActual;
    @BindView(R.id.ll_pay_voucher)
    LinearLayout mLlPayVoucher;
    @BindView(R.id.ll_pay_image)
    LinearLayout mLlPayImage;
    @BindView(R.id.btn_submit)
    Button mBtnSubmit;
    @BindView(R.id.ll_type)
    LinearLayout mLlType;
    @BindView(R.id.ll_level)
    LinearLayout mLlLevel;
    @BindView(R.id.ll_region)
    LinearLayout mLlRegion;
    @BindView(R.id.ll_deposit_price)
    LinearLayout mLlDepositPrice;
    @BindView(R.id.ll_pay_wait)
    LinearLayout mLlPayWait;
    @BindView(R.id.ll_back_price)
    LinearLayout mLlBackPrice;
    @BindView(R.id.ll_use_points)
    LinearLayout mLlUsePoints;
    @BindView(R.id.ll_points)
    LinearLayout mLlPoints;
    @BindView(R.id.ll_pay_actual)
    LinearLayout mLlPaidPrice;
    @BindView(R.id.ll_pay)
    LinearLayout mLlPayInfo;
    @BindView(R.id.ll_refund_image)
    LinearLayout mLlRefundImage;
    @BindView(R.id.tv_label_pay_image)
    TextView mTvPayImageLabel;
    @BindView(R.id.rv_user_upload)
    RecyclerView mRvUserUpload;
    @BindView(R.id.rv_image_refund)
    RecyclerView mRvImageRefund;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_account)
    TextView mTvAccount;
    @BindView(R.id.ll_payee_name)
    LinearLayout mLlPayeeName;
    @BindView(R.id.ll_payee_account)
    LinearLayout mLlPayeeAccount;
    @BindView(R.id.tv_label_tips)
    TextView mTvLabelTips;
    @BindView(R.id.tv_tips)
    TextView mTvTips;
    @BindView(R.id.iv_right_pay)
    ImageView mIvRightPay;
    @BindView(R.id.iv_right_point)
    ImageView mIvRightPoint;

    private UpgradeBean mUpgradeBean;
    private UpgradeDetailBean mUpgradeDetailBean;
    private PopupWindow mUsePointPopupWindow;
    private ArrayList<ImageItem> mImageList = new ArrayList<>();
    private ArrayList<ImageItem> mRefundImageList = new ArrayList<>();
    private double mPointCovert = 0;
    private String mPayeeId;

    private CommonAdapter<ImageItem> mPayImageAdapter;
    private CommonAdapter<ImageItem> mRefundImageAdapter;
    private boolean mShowDelete = false;
    private String mLevel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_apply_detail);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.order_detail));

        mUpgradeBean = (UpgradeBean) getIntent().getSerializableExtra("UpgradeBean");

        initImageRv();
        initRefundImageRv();

        setView(mUpgradeBean.getType(), mUpgradeBean.getState());
        setLevelE(mUpgradeBean.getToLevel());

        httpGetUpgradeDetail(mUpgradeBean.getId());
    }

    private void setPointEditText() {
        mEtPoints.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String pointStr = mEtPoints.getText().toString();
                if (mUpgradeDetailBean != null && !TextUtils.isEmpty(pointStr)) {
                    Integer point = Integer.valueOf(pointStr);
                    UserBean loginUser = UserBizFactory.createBiz().getLoginUser(mContext);
                    if (point > loginUser.getPoints()) {
                        mEtPoints.setText(loginUser.getPoints() + "");
                        mEtPoints.setSelection(mEtPoints.getText().toString().length());
                        ToastUtil.showMessage(mContext, "可使用积分最多为" + loginUser.getPoints());
                        return;
                    }

                    double realPay = mUpgradeDetailBean.getTotalPrice() - mPointCovert * point;
                    mTvPayActual.setText(realPay + "");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x262 && resultCode == Activity.RESULT_OK) {
            PayeeMethodBean payeeMethodBean = (PayeeMethodBean) data.getSerializableExtra("PayeeMethodBean");
            setPayeeView(payeeMethodBean);
        }

        if (requestCode == 0x268 && resultCode == Activity.RESULT_OK) {
            ArrayList<ImageItem> imageList = data.getParcelableArrayListExtra("ImageList");
            if (imageList != null) {
                mImageList.clear();
                mImageList.addAll(imageList);
                mPayImageAdapter.notifyDataSetChanged();
                mLlPayImage.setVisibility(View.VISIBLE);
            }

        }
    }

    @OnClick(R.id.ll_pay_type)
    void payeeSelectClick() {
        Intent intent = new Intent(mContext, CollectionWayActivity.class);
        intent.putExtra("isCompany", true);
        intent.putExtra("PayeeId", mPayeeId);
        startActivityForResult(intent, 0x262);
    }

    @OnClick(R.id.ll_use_points)
    void usePointsClick() {
        showUsePointsPopupWindow();
    }

    @OnClick(R.id.ll_pay_voucher)
    void uploadPayImageClick() {
        Intent intent = new Intent(mContext, PaymentVoucherActivity.class);
        intent.putExtra("ImageList", mImageList);
        startActivityForResult(intent, 0x268);
    }


    @OnClick(R.id.btn_submit)
    void submitClick() {
        if (!StringUtil.isEquals(mLevel, "E") && TextUtils.isEmpty(mPayeeId)) {
            ToastUtil.showMessage(mContext, "请选择收款渠道");
            return;
        }
        if (!StringUtil.isEquals(mLevel, "E") && mImageList.isEmpty()) {
            ToastUtil.showMessage(mContext, "请上传付款凭证");
            return;
        }

        httpCommitUploadFile(mImageList);
    }

    private void setPayeeView(PayeeMethodBean payeeMethodBean) {
        if (payeeMethodBean == null) {
            mLlPayeeInfo.setVisibility(View.GONE);
            return;
        }
        mLlPayeeInfo.setVisibility(View.VISIBLE);
        mLlPayeeName.setVisibility(View.VISIBLE);
        mLlPayeeAccount.setVisibility(View.VISIBLE);
        mPayeeId = payeeMethodBean.getId();
        mTvPayment.setText(payeeMethodBean.getBank());
        mTvName.setText(payeeMethodBean.getName());
        mTvAccount.setText(payeeMethodBean.getAccount());
    }

    private void initImageRv() {
        mPayImageAdapter = new CommonAdapter<ImageItem>(mContext, mImageList, R.layout.item_grid_image_view) {
            @Override
            protected void convert(final ViewHolder holder, final ImageItem imageItem, final int position) {
                ImageView ivItem = (ImageView) holder.getView(R.id.iv_item);

                if (TextUtils.isEmpty(imageItem.url)) {
                    ImageLoader.display(mContext, new File(imageItem.path), ivItem);
                }

                if (TextUtils.isEmpty(imageItem.path)) {
                    ImageLoader.display(mContext, imageItem.url, ivItem);
                }

                ivItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mImageList.get(position).path) && TextUtils.isEmpty(mImageList.get(position).url)) {
                            return;
                        }
                        Intent intentPreview = new Intent(mContext, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageList);
                        startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                    }
                });

                if (mShowDelete) {
                    holder.getView(R.id.iv_delete).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                }
            }


        };


        mRvUserUpload.setLayoutManager(new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvUserUpload.setAdapter(mPayImageAdapter);
        mRvUserUpload.setHasFixedSize(true);
        mRvUserUpload.setNestedScrollingEnabled(false);
    }


    private void initRefundImageRv() {
        mRefundImageAdapter = new CommonAdapter<ImageItem>(mContext, mRefundImageList, R.layout.item_grid_image_view) {
            @Override
            protected void convert(final ViewHolder holder, final ImageItem imageItem, final int position) {
                holder.setImageByUrl(mContext, R.id.iv_item, imageItem.url);
                ImageView ivItem = (ImageView) holder.getView(R.id.iv_item);
                ivItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intentPreview = new Intent(LevelApplyDetailActivity.this, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mRefundImageList);
                        startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                    }
                });
            }
        };


        mRvImageRefund.setLayoutManager(new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvImageRefund.setAdapter(mRefundImageAdapter);
    }


    private void setView(String type, String state) {
        switch (type) {
            case StateUtil.UPGRADE:
                mLlBackPrice.setVisibility(View.GONE);
                if (StringUtil.isEquals(state, StateUtil.UPGRADE_PAY)) {
                    setPayState();
                } else {
                    mBtnSubmit.setVisibility(View.GONE);
                    mLlCollectionWay.setEnabled(false);
                    mLlUsePoints.setEnabled(false);
                    mLlPayVoucher.setEnabled(false);
                    mLlPayVoucher.setVisibility(View.GONE);
                    mIvRightPay.setVisibility(View.GONE);
                    mIvRightPoint.setVisibility(View.GONE);
                }
                mLlPayeeInfo.setVisibility(View.VISIBLE);
                break;
            case StateUtil.DEGRADE:
                mLlPayWait.setVisibility(View.GONE);
                mTvPayImageLabel.setVisibility(View.GONE);
                mLlPayInfo.setVisibility(View.GONE);
                mLlPayeeInfo.setVisibility(View.GONE);
                mLlBackPrice.setVisibility(View.VISIBLE);
                mIvRightPay.setVisibility(View.GONE);
                mIvRightPoint.setVisibility(View.GONE);
                break;
            case StateUtil.TO_AGENT:
                mTvLabelTips.setText("转为代理说明：");
                if (StringUtil.isEquals(state, StateUtil.UPGRADE_PAY)) {
                    setPayState();
                } else {
                    mBtnSubmit.setVisibility(View.GONE);
                    mLlCollectionWay.setEnabled(false);
                    mLlUsePoints.setEnabled(false);
                    mLlPayVoucher.setEnabled(false);
                    mLlPayVoucher.setVisibility(View.GONE);
                    mIvRightPay.setVisibility(View.GONE);
                    mIvRightPoint.setVisibility(View.GONE);
                }
                mLlType.setVisibility(View.GONE);
                mLlPayeeInfo.setVisibility(View.VISIBLE);
                mLlDepositPrice.setVisibility(View.VISIBLE);
                mLlPayWait.setVisibility(View.GONE);
                mLlBackPrice.setVisibility(View.GONE);
                break;
            default:
                break;

        }

        RulesMapBean rulesMapBean = CommonUtil.getRulesMap(this);
        if (rulesMapBean != null && rulesMapBean.getRULE_UPGRADE() != null) {
            if (StringUtil.isEquals(type, StateUtil.TO_AGENT)) {
                mTvTips.setText(rulesMapBean.getRULE_TO_AGENT());
            } else {
                mTvTips.setText(rulesMapBean.getRULE_UPGRADE());
            }
        }
    }

    private void setPayState() {
        mLlPayeeName.setVisibility(View.GONE);
        mLlPayeeAccount.setVisibility(View.GONE);
        if (mImageList.isEmpty()) {
            mLlPayImage.setVisibility(View.GONE);
        } else {
            mLlPayImage.setVisibility(View.VISIBLE);
        }

        mBtnSubmit.setVisibility(View.VISIBLE);
        mLlRefundImage.setVisibility(View.GONE);
        httpGetPointConvert();
        mPayImageAdapter.notifyDataSetChanged();
        setCanUsePoints();

        setPointEditText();
    }

    private void setLevelE(String level) {
        if (level == null) {
            return;
        }
        mLevel = level;
        if (StringUtil.isEquals(level, "E")) {
            mLlPayeeInfo.setVisibility(View.GONE);

            mLlUsePoints.setVisibility(View.GONE);
            mLlPoints.setVisibility(View.GONE);
            mLlPoints.setVisibility(View.GONE);
        }
    }

    private void setCanUsePoints() {
        UserBean userBean = UserBizFactory.createBiz().getLoginUser(mContext);
        mEtPoints.setHint(getString(R.string.format_max_points_use, userBean.getPoints()));
        httpGetMyPoints();
    }

    private void setDetailView(UpgradeDetailBean upgradeDetailBean) {
        if (upgradeDetailBean == null) {
            return;
        }

        mTvOrderCode.setText(getString(R.string.format_upgrade_order_no, upgradeDetailBean.getCode()));
        mTvTime.setText(getString(R.string.format_upgrade_time, DateUtil.getDateTimeString(upgradeDetailBean.getCreatedOn())));
        if (StringUtil.isEquals(StateUtil.TO_AGENT, upgradeDetailBean.getType())) {
            mTvType.setVisibility(View.GONE);
        }
        mTvType.setText(StateUtil.getUpgradeType(this, upgradeDetailBean.getType()));
        mTvLevel.setText(StateUtil.getLevelText(upgradeDetailBean.getToLevel()));
        mTvRegion.setText(CommonUtil.getRegionString(upgradeDetailBean.getToRegion()));
        if (upgradeDetailBean.getPayeeInfo() != null) {
            mTvPayment.setText(upgradeDetailBean.getPayeeInfo().getBank());
        }
        setPayeeView(upgradeDetailBean.getPayeeInfo());
        mTvDepositPrice.setText(getString(R.string.format_price, upgradeDetailBean.getDepositPrice()));
        mTvPayWait.setText(getString(R.string.format_price, upgradeDetailBean.getTotalPrice()));
        mTvRefundPrice.setText(getString(R.string.format_price, upgradeDetailBean.getTotalPrice()));
        mTvPayActual.setText(getString(R.string.format_price, upgradeDetailBean.getPaidPrice()));

        if (upgradeDetailBean.getPoints() != null) {
            mEtPoints.setText(String.valueOf(upgradeDetailBean.getPoints()));
            mTvUsePoints.setText("是");
            mLlPoints.setVisibility(View.VISIBLE);
        } else {
            mTvUsePoints.setText("否");
            mLlPoints.setVisibility(View.GONE);
        }
        String fid = mUpgradeDetailBean.getPaymentFid();
        mImageList.clear();
        if (!TextUtils.isEmpty(fid)) {
            mTvPayImageLabel.setVisibility(View.VISIBLE);
            String[] strings = fid.split(",");
            for (String string : strings) {
                ImageItem imageItem = new ImageItem(string, System.currentTimeMillis());
                imageItem.url = Url.FILE_DOWNLOAD + string;
                mImageList.add(imageItem);
            }
        } else {
            mLlPayImage.setVisibility(View.GONE);
        }

        mPayImageAdapter.notifyDataSetChanged();

        String refundFid = mUpgradeDetailBean.getRefundFid();
        mRefundImageList.clear();
        if (!TextUtils.isEmpty(refundFid)) {
            mLlRefundImage.setVisibility(View.VISIBLE);
            String[] strings = refundFid.split(",");
            for (String string : strings) {
                ImageItem imageItem = new ImageItem(string, System.currentTimeMillis());
                imageItem.url = Url.FILE_DOWNLOAD + string;
                mRefundImageList.add(imageItem);
            }
            mRefundImageAdapter.notifyDataSetChanged();
        } else {
            mLlRefundImage.setVisibility(View.GONE);
        }

        setView(upgradeDetailBean.getType(), upgradeDetailBean.getState());
        setLevelE(upgradeDetailBean.getToLevel());
    }


    private void showUsePointsPopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_is_use_point, null);
        // 强制隐藏键盘
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        if (mUsePointPopupWindow == null) {
            mUsePointPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mUsePointPopupWindow.setFocusable(true);
            mUsePointPopupWindow.setOutsideTouchable(true);
            /* mUsePointPopupWindow.setBackgroundDrawable(new BitmapDrawable());*/
            mUsePointPopupWindow.setAnimationStyle(R.style.AnimationBottomDialog);
            mUsePointPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mUsePointPopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            mUsePointPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    WindowUtil.setPopWindowBackgroundAlpha(LevelApplyDetailActivity.this, 1f);
                }
            });

            view.findViewById(R.id.tv_yes).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUsePointPopupWindow.dismiss();
                    mTvUsePoints.setText("是");
                    mLlPoints.setVisibility(View.VISIBLE);
                }
            });
            view.findViewById(R.id.tv_no).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUsePointPopupWindow.dismiss();
                    mTvUsePoints.setText("否");
                    mLlPoints.setVisibility(View.GONE);
                    mEtPoints.setText("0");
                    mEtPoints.setSelection(mEtPoints.getText().toString().length());
                }
            });

            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUsePointPopupWindow.dismiss();
                }
            });


        }
        WindowUtil.setPopWindowBackgroundAlpha(LevelApplyDetailActivity.this, 0.7f);
        mUsePointPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }


    private void httpGetUpgradeDetail(String upgradeId) {
        try {
            UpgradeBizFactory.createBiz().getUpgradeDetail(this, upgradeId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    mUpgradeDetailBean = (UpgradeDetailBean) data;
                    setDetailView(mUpgradeDetailBean);

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }


    private void httpGetPointConvert() {
        try {
            CommonFactory.createBiz().pointConvert(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    mPointCovert = (double) data;
                    if (mUpgradeDetailBean != null) {
                        double realPay = mUpgradeDetailBean.getTotalPrice();
                        mTvPayActual.setText(realPay + "");
                    }

                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void httpCommitUploadFile(ArrayList<ImageItem> customImageList) {
        final ArrayList<File> fileList = new ArrayList<>();


        for (ImageItem imageItem : customImageList) {
            if (!TextUtils.isEmpty(imageItem.path)) {
                fileList.add(new File(imageItem.path));
            }
        }
        try {
            FileBizFactory.createBiz().uploads(this, fileList, Url.FILE_TYPE_IMAGE, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    List<UploadFileBean> fileBeanList = (List<UploadFileBean>) data;
                    List<String> fileIds = new ArrayList<>();
                    for (UploadFileBean uploadFileBean : fileBeanList) {
                        fileIds.add(uploadFileBean.getId());
                    }

                    String ids = StringUtil.arrayList(fileIds);

                    httpCommit(ids);
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();
        }

    }


    private void httpCommit(String fid) {
        String points = null;
        if (mLlPoints.isShown() && !TextUtils.isEmpty(mEtPoints.getText().toString())) {
            points = mEtPoints.getText().toString().trim();
        }
        try {
            UpgradeBizFactory.createBiz().upgradePay(this, mPayeeId, fid, points, mUpgradeBean.getId(), new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    //                    httpGetUpgradeDetail(mUpgradeBean.getId());
                    ToastUtil.showMessage(mContext, "提交成功");
                    dismissLoadingDialog();
                    setResult(Activity.RESULT_OK);
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void httpGetMyPoints() {
        try {
            UserBizFactory.createBiz().getMyPoint(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    Integer points = (Integer) data;
                    if (points != null) {
                        mEtPoints.setHint(getString(R.string.format_max_points_use, points));
                        UserBean userBean = UserBizFactory.createBiz().getLoginUser(mContext);
                        userBean.setPoints(points);
                        DBUtil.getInstance(mContext).save(userBean);

                    }
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
